package com.mall.users.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.model.domain.*;
import com.mall.model.vo.CommonPage;
import com.mall.users.interceptor.UmsAdminHolder;
import com.mall.users.mapper.UmsAdminMapper;
import com.mall.users.mapper.UmsRoleMapper;
import com.mall.users.mapper.UmsRoleResourceRelationMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UmsRoleService {

    @Autowired
    private UmsRoleMapper umsRoleMapper;

    @Autowired
    private UmsAdminRoleRelationService umsAdminRoleRelationService;

    @Autowired
    private UmsRoleMenuRelationService umsRoleMenuRelationService;

    @Autowired
    private UmsMenuService umsMenuService;

    @Autowired
    private UmsRoleResourceRelationService umsRoleResourceRelationService;

    @Autowired
    private UmsResourceService umsResourceService;



    /**
     * 根据角色名称分页获取角色列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public CommonPage list(Integer pageNum, Integer pageSize, String keyword) {
        Integer count = umsRoleMapper.selectCount(keyword);
        List<UmsRole> umsRoleList = umsRoleMapper.selectByPage(pageNum,pageSize,keyword);
        return new CommonPage(pageNum, pageSize, count, umsRoleList);
    }

    /**
     * 根据roleId查询对象集合
     * @param roleIds
     * @return
     */
    public List<UmsRole> findByIds(List<Long> roleIds) {
        return umsRoleMapper.selectByIds(roleIds);
    }

    /**
     * 获取所有角色列表
     * @return
     */
    public List<UmsRole>  listAll() {
        return umsRoleMapper.selectAll();
    }

    /**
     * 修改角色
     * @param umsRole
     */
    public void update(UmsRole umsRole) {
        umsRoleMapper.update(umsRole);
    }

    /**
     * 添加角色
     * @param umsRole
     */
    public void save(UmsRole umsRole) {
        UmsRole role = umsRoleMapper.selectByName(umsRole.getName());
        if (role!= null && role.getId() != umsRole.getId()) {
            throw new RuntimeException("角色名称已存在");
        }
        umsRole.setCreateTime(new Date());
        umsRoleMapper.save(umsRole);
    }

    /**
     * 批量删除角色
     * @param ids
     */
    @Transactional
    public void delete(List<Long> ids) {
        //删除ums_role表
        umsRoleMapper.delete(ids);
        //删除对应用户关系表
        umsAdminRoleRelationService.deleteByRoleIds(ids);
        //删除对应菜单关系表
        umsRoleMenuRelationService.deleteByRoleIds(ids);

    }

    /**
     * 根据角色id获取相关菜单
     * @param roleId
     * @return
     */
    public List<UmsMenu> listMenu(Long roleId) {
        List<Long> menuIds = umsRoleMenuRelationService.findByRoleIds(CollUtil.toList(roleId));
        if (CollUtil.isEmpty(menuIds)) {
            return new ArrayList<>();
        }
        List<UmsMenu> umsMenuList = umsMenuService.selectByIds(menuIds);
        return umsMenuList;
    }

    /**
     * 根据角色id获取相关资源
     * @param roleId
     * @return
     */
    public List<UmsResource> listResource(Long roleId) {
        List<Long> resourceIds = umsRoleResourceRelationService.findByRoleIds(CollUtil.toList(roleId));
        if (CollUtil.isEmpty(resourceIds)) {
            return new ArrayList<>();
        }
        List<UmsResource> umsResourceList = umsResourceService.selectByIds(resourceIds);
        return umsResourceList;
    }

    /**
     * 新增菜单和角色的对应关系
     * @param roleId
     * @param menuIds
     */
    public void allocMenu(Long roleId, List<Long> menuIds) {
        umsRoleMenuRelationService.save(roleId, menuIds);
    }

    /**
     * 修改角色状态
     */
    public void updateStatus(Long id, Integer status) {
        umsRoleMapper.updateStatus(id, status);
    }

    /**
     * 新增资源和角色的对应关系
     * @param roleId
     * @param resourceIds
     */
    public void allocResource(Long roleId, List<Long> resourceIds) {
        umsRoleResourceRelationService.save(roleId, resourceIds);
    }
}
